<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="酒店名称" prop="hotelName">
        <el-input
          v-model="queryParams.hotelName"
          placeholder="请输入酒店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="酒店状态" prop="status">
        <el-select v-model="queryParams.status" style="width: 120px" placeholder="请选择" clearable>
          <el-option
            v-for="dict in sys_hotel_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:hotel_info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:hotel_info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:hotel_info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:hotel_info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="酒店名称" align="center" prop="hotelName" />
      <el-table-column label="酒店介绍" show-overflow-tooltip align="center" prop="hotelDesc" />
      <el-table-column label="酒店管理员" align="center" prop="managerId" />
      <el-table-column label="房间总数" align="center" prop="totalRooms" />
      <el-table-column label="可用房间数" align="center" prop="availableRooms" />
      <el-table-column label="酒店客服电话" align="center" prop="customerServicePhone" />
      <el-table-column label="最低消费" align="center" prop="originalPrice" />
      <el-table-column label="当前价格" align="center" prop="currentPrice" />
      <el-table-column label="酒店地址" show-overflow-tooltip align="center" prop="address" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_hotel_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <view style="display: flex !important; flex-direction: column !important; gap: 8px; align-items: center; justify-content: center">
            <view>
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:hotel_info:edit']">修改</el-button>
            </view>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:hotel_info:remove']">删除</el-button>
            <router-link :to="'/system/hotel_info-details/index/' + scope.row.hotelId" class="link-type">
                <el-button link type="primary" icon="EditPen" v-hasPermi="['system:hotel_info:edit']">主体详情</el-button>
            </router-link>
          </view>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改酒店信息管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="酒店名称" prop="hotelName">
          <el-input v-model="form.hotelName" placeholder="请输入酒店名称" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="酒店介绍" prop="hotelDesc">
          <el-input v-model="form.hotelDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="客服电话" prop="customerServicePhone">
          <el-input v-model="form.customerServicePhone" placeholder="请输入酒店客服电话" />
        </el-form-item>
        <el-form-item label="最低消费" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入酒店最低消费" />
        </el-form-item>
        <el-form-item label="当前消费" prop="currentPrice">
          <el-input v-model="form.currentPrice" placeholder="请输入酒店最低消费" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in sys_hotel_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理员" prop="managerId">
          <el-input v-model="form.managerId" readonly placeholder="请输入酒店管理员" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/system/info"
import useUserStore from '@/store/modules/user'
const userStore = useUserStore();

const { proxy } = getCurrentInstance()
const { sys_hotel_status } = proxy.useDict('sys_hotel_status')

const infoList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hotelName: null,
    address: null,
    hotelDesc: null,
    managerId: null,
    totalRooms: null,
    availableRooms: null,
    customerServicePhone: null,
    status: null,
    businessLicense: null,
    hotelTypeId: null,
    scenicAreaId: null,
    currentPrice: null,
    originalPrice: null
  },
  rules: {
    hotelName: [
      { required: true, message: "酒店名称不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "详细地址不能为空", trigger: "blur" }
    ],
    hotelDesc: [
      { required: true, message: "酒店介绍不能为空", trigger: "blur" }
    ],
    managerId: [
      { required: true, message: "酒店管理员id不能为空", trigger: "blur" }
    ],
    customerServicePhone: [
      { required: true, message: "酒店客服电话不能为空", trigger: "blur" }
    ],
    hotelTypeId: [
      { required: true, message: "酒店类型id不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

// function handleDetails(){
//   // 跳转到主体详情页面
//
// }

/** 查询酒店信息管理列表 */
function getList() {
  loading.value = true
  console.log()
  listInfo({...queryParams.value, managerId: userStore.id}).then(response => {
    infoList.value = response.rows
    infoList.value.forEach(item => {
      item.managerId = userStore.nickName
    })
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    hotelId: null,
    hotelName: null,
    address: null,
    hotelDesc: null,
    managerId: null,
    totalRooms: null,
    availableRooms: null,
    customerServicePhone: null,
    status: null,
    businessLicense: null,
    hotelTypeId: null,
    scenicAreaId: null,
    createTime: null,
    updateTime: null,
    currentPrice: null,
    originalPrice: null
  }
  proxy.resetForm("infoRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.hotelId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  console.log(userStore)
  form.value.managerId = userStore.nickName
  open.value = true
  title.value = "添加酒店信息管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _hotelId = row.hotelId || ids.value
  getInfo(_hotelId).then(response => {
    form.value = response.data
    form.value.managerId = userStore.nickName
    open.value = true
    title.value = "修改酒店信息管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.hotelId != null) {
        updateInfo({...form.value, managerId: userStore.id}).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        form.value.managerId = userStore.id
        addInfo({...form.value, managerId: userStore.id}).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  console.log(row)
  const _hotelIds = row.hotelId || ids.value
  console.log(_hotelIds)
  proxy.$modal.confirm('是否确认删除酒店信息管理编号为"' + _hotelIds + '"的数据项？').then(function() {
    return delInfo(_hotelIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList()
})
</script>
